'use strict';

const os = require('os');
const moment = require('moment');
const async = require('async');
const { sequelize } = require('sequelize');

const Controller = require('../core/common');

class TesterController extends Controller {

    async index() {
        const { ctx, app } = this;

        // const { goodsId } = this.request.query;
        const data = await ctx.model.GoodsPrizes.sum('number', {
            where: { goodsId: ctx.request.query.goodsId },
        })
        return this.success(data)

        const { userId, goodsId, goodsPrizeId, number = 1 } = ctx.request.query;

        const usersArray = await ctx.model.UsersGoods.findAll({
            // attributes: ['userId'],
            where: { goodsId, goodsPrizeId },
            order: [
                ['number', 'DESC']
            ],
            limit: 1
        });
        return this.success(usersArray)
        await app.model.query(
            `SELECT * FROM Users`
        ).then(res => {
            console.log(res, 111)
        })

        // console.log(app.Sequelize, 111)

        return this.success(123)

        // const data = await new Promise((resolve, reject) => {
        //   const terraceList = [1, 2, 3, 4, 5, 6];
        //   const terraceCount = [];
        //   async.each(terraceList, (item, callback) => {
        //     app.redis.get('goods').get(`goodsUsers_pv_4`, (err, data) => {
        //       if (err) throw err;
        //       terraceCount.push(data);
        //       callback(null);
        //     })
        //   }, err => err ? reject(err) : resolve(terraceCount));
        // });

        // this.success(data);
        // this.ctx.validate({
        //   status: { type: 'number' },
        //   content: { type: 'string' },
        // },ctx.request.query);

        // const aaa = ctx.app.jwt.sign({
        //   data: {
        //     _id: 1
        //   },
        //   exp: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 7)
        // }, ctx.app.config.jwt.secret)
        // const hash = await ctx.genHash('xiazhi123456');
        // console.log(hash)
        // console.log(ctx.compare(1, 1), 'aaaaaaaaa')
        // const { startedAt } = ctx.request.query;
        // await app.redis.get('expired').set(`goods_endAt_id_1`, 'data.id');
        // console.log(String(startedAt), moment(String(startedAt)).unix(), (Date.now() / 1000), 111)
        // await app.redis.get('expired').expire(`goods_endAt_id_1`, moment(String(startedAt)).unix() - (Date.now() / 1000));

        // const id = 1;
        // await app.redis.get('expired').set(`banner_endAt_id_${id}`, '');
        // await app.redis.get('expired').expire(`banner_endAt_id_${id}`, 5);
    }
}

module.exports = TesterController;